package com.warom.sdg.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.warom.sdg.database.SdgDatabase;
import com.warom.sdg.model.UserEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户数据访问对象实现类
 * 实现用户表的数据库操作方法
 */
public class UserDao_Impl implements UserDao {
    private static final String TAG = "UserDao_Impl";
    
    private final SdgDatabase database;
    
    /**
     * 构造函数
     * @param database 数据库实例
     */
    public UserDao_Impl(SdgDatabase database) {
        this.database = database;
    }
    
    @Override
    public long insert(UserEntity user) {
        SQLiteDatabase db = database.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        values.put(SdgDatabase.COLUMN_USERNAME, user.getUsername());
        values.put(SdgDatabase.COLUMN_PASSWORD, user.getPassword());
        values.put(SdgDatabase.COLUMN_USER_NAME, user.getName());
        values.put(SdgDatabase.COLUMN_ROLE, user.getRole());
        values.put(SdgDatabase.COLUMN_LAST_LOGIN_TIME, user.getLastLoginTime());
        
        long id = db.insert(SdgDatabase.TABLE_USERS, null, values);
        Log.d(TAG, "插入用户成功，ID: " + id);
        return id;
    }
    
    @Override
    public void update(UserEntity user) {
        SQLiteDatabase db = database.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        values.put(SdgDatabase.COLUMN_USERNAME, user.getUsername());
        values.put(SdgDatabase.COLUMN_PASSWORD, user.getPassword());
        values.put(SdgDatabase.COLUMN_USER_NAME, user.getName());
        values.put(SdgDatabase.COLUMN_ROLE, user.getRole());
        values.put(SdgDatabase.COLUMN_LAST_LOGIN_TIME, user.getLastLoginTime());
        
        db.update(
                SdgDatabase.TABLE_USERS,
                values,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(user.getId())}
        );
        Log.d(TAG, "更新用户成功，ID: " + user.getId());
    }
    
    @Override
    public void delete(UserEntity user) {
        SQLiteDatabase db = database.getWritableDatabase();
        db.delete(
                SdgDatabase.TABLE_USERS,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(user.getId())}
        );
        Log.d(TAG, "删除用户成功，ID: " + user.getId());
    }
    
    @Override
    public List<UserEntity> getAllUsers() {
        List<UserEntity> users = new ArrayList<>();
        SQLiteDatabase db = database.getReadableDatabase();
        
        Cursor cursor = db.query(
                SdgDatabase.TABLE_USERS,
                null,
                null,
                null,
                null,
                null,
                SdgDatabase.COLUMN_ID + " ASC"
        );
        
        try {
            if (cursor.moveToFirst()) {
                do {
                    UserEntity user = new UserEntity();
                    user.setId(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_ID)));
                    user.setUsername(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USERNAME)));
                    user.setPassword(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_PASSWORD)));
                    user.setName(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USER_NAME)));
                    user.setRole(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_ROLE)));
                    user.setLastLoginTime(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_LAST_LOGIN_TIME)));
                    
                    users.add(user);
                } while (cursor.moveToNext());
            }
        } finally {
            cursor.close();
        }
        
        Log.d(TAG, "获取到 " + users.size() + " 个用户");
        return users;
    }
    
    @Override
    public UserEntity getUserById(long id) {
        SQLiteDatabase db = database.getReadableDatabase();
        
        Cursor cursor = db.query(
                SdgDatabase.TABLE_USERS,
                null,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)},
                null,
                null,
                null
        );
        
        UserEntity user = null;
        
        try {
            if (cursor.moveToFirst()) {
                user = new UserEntity();
                user.setId(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_ID)));
                user.setUsername(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USERNAME)));
                user.setPassword(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_PASSWORD)));
                user.setName(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USER_NAME)));
                user.setRole(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_ROLE)));
                user.setLastLoginTime(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_LAST_LOGIN_TIME)));
            }
        } finally {
            cursor.close();
        }
        
        return user;
    }
    
    @Override
    public UserEntity getUserByUsername(String username) {
        SQLiteDatabase db = database.getReadableDatabase();
        
        Cursor cursor = db.query(
                SdgDatabase.TABLE_USERS,
                null,
                SdgDatabase.COLUMN_USERNAME + " = ?",
                new String[]{username},
                null,
                null,
                null
        );
        
        UserEntity user = null;
        
        try {
            if (cursor.moveToFirst()) {
                user = new UserEntity();
                user.setId(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_ID)));
                user.setUsername(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USERNAME)));
                user.setPassword(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_PASSWORD)));
                user.setName(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USER_NAME)));
                user.setRole(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_ROLE)));
                user.setLastLoginTime(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_LAST_LOGIN_TIME)));
            }
        } finally {
            cursor.close();
        }
        
        return user;
    }
    
    @Override
    public UserEntity verifyLogin(String username, String password) {
        SQLiteDatabase db = database.getReadableDatabase();
        
        Cursor cursor = db.query(
                SdgDatabase.TABLE_USERS,
                null,
                SdgDatabase.COLUMN_USERNAME + " = ? AND " + SdgDatabase.COLUMN_PASSWORD + " = ?",
                new String[]{username, password},
                null,
                null,
                null
        );
        
        UserEntity user = null;
        
        try {
            if (cursor.moveToFirst()) {
                user = new UserEntity();
                user.setId(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_ID)));
                user.setUsername(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USERNAME)));
                user.setPassword(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_PASSWORD)));
                user.setName(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_USER_NAME)));
                user.setRole(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_ROLE)));
                user.setLastLoginTime(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_LAST_LOGIN_TIME)));
                
                // 更新最后登录时间
                updateLastLoginTime(user.getId(), System.currentTimeMillis());
            }
        } finally {
            cursor.close();
        }
        
        return user;
    }
    
    @Override
    public void updateLastLoginTime(long id, long lastLoginTime) {
        SQLiteDatabase db = database.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        values.put(SdgDatabase.COLUMN_LAST_LOGIN_TIME, lastLoginTime);
        
        db.update(
                SdgDatabase.TABLE_USERS,
                values,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)}
        );
        
        Log.d(TAG, "更新用户最后登录时间成功，ID: " + id);
    }
    
    @Override
    public void deleteAll() {
        SQLiteDatabase db = database.getWritableDatabase();
        db.delete(SdgDatabase.TABLE_USERS, null, null);
        Log.d(TAG, "删除所有用户成功");
    }
} 